package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.personal.SettingsActivity;
import com.zhongjian.technologycal.mall.adapter.WithdrawAccountAdapter;
import com.zhongjian.technologycal.mall.adapter.WithdrawAccountIdCardAdapter;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.bean.PayAccountBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.drakeet.materialdialog.MaterialDialog;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/25.
 * 选择提现账户(提现账户列表)
 */

public class WithdrawAccountActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.add_withdraw_ll_account)
    LinearLayout add_withdraw_ll_account;
    @BindView(R.id.add_withdraw_listview)
    ListViewForScrollView add_withdraw_listview;
    @BindView(R.id.add_withdraw_listview1)
    ListViewForScrollView add_withdraw_listview1;
    private WithdrawAccountAdapter withdrawAccountAdapter;
    private WithdrawAccountIdCardAdapter withdrawAccountIdCardAdapter;
    private List<PayAccountBean> listItems = new ArrayList<>();
    private List<PayAccountBean> listItems1 = new ArrayList<>();
    private MaterialDialog materialDialog = null;
    private int type = 0;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    /**
     * 初始化数据
     * @param savedInstanceState
     */
    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("选择提现账号");
        withdrawAccountAdapter = new WithdrawAccountAdapter(getBaseContext(), listItems);
        add_withdraw_listview.setAdapter(withdrawAccountAdapter);
        add_withdraw_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent();
                intent.putExtra("username", listItems.get(position).getData().getAliAccount().get(position).getName());
                intent.putExtra("useraccount", listItems.get(position).getData().getAliAccount().get(position).getAccount());
                intent.putExtra("type", 0);
                setResult(180, intent);
                finish();
            }
        });
        add_withdraw_listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
                materialDialog = new MaterialDialog(WithdrawAccountActivity.this)
                        .setMessage("确定删除这条数据吗？")
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                initViewDeleteAliAccount(listItems.get(position).getData().getAliAccount().get(position).getId());
                                materialDialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                materialDialog.dismiss();
                            }
                        });
                materialDialog.setCanceledOnTouchOutside(true);
                materialDialog.show();
                return true;
            }
        });

        withdrawAccountIdCardAdapter = new WithdrawAccountIdCardAdapter(getBaseContext(), listItems1);
        add_withdraw_listview1.setAdapter(withdrawAccountIdCardAdapter);
        add_withdraw_listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent();
                intent.putExtra("username", listItems1.get(position).getData().getBankAccount().get(position).getName());
                intent.putExtra("bankname", listItems1.get(position).getData().getBankAccount().get(position).getBankname());
                intent.putExtra("useraccount", listItems1.get(position).getData().getBankAccount().get(position).getAccount());
                intent.putExtra("banklogo", listItems1.get(position).getData().getBankAccount().get(position).getBanklogo());
                intent.putExtra("bankInfo", listItems1.get(position).getData().getBankAccount().get(position).getBankinfo());
                intent.putExtra("type", 1);
                setResult(181, intent);
                finish();
            }
        });
        add_withdraw_listview1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
                materialDialog = new MaterialDialog(WithdrawAccountActivity.this)
                        .setMessage("确定删除这条数据吗？")
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                initViewDeleteBankAccount(listItems1.get(position).getData().getBankAccount().get(position).getId());
                                materialDialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                materialDialog.dismiss();
                            }
                        });
                materialDialog.setCanceledOnTouchOutside(true);
                materialDialog.show();
                return true;
            }
        });


    }

    @Override
    public void initPresenter() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).getUrlRequestGetAllAccount(UserUtils.getUserToken(),
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();

                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            Gson gson = new Gson();
                            PayAccountBean payAccountBean = gson.fromJson(responseBody.string(), PayAccountBean.class);
                            List<PayAccountBean> forList = new ArrayList<>();
                            List<PayAccountBean> forList1 = new ArrayList<>();
                            listItems.clear();
                            listItems1.clear();
                            for (int i = 0; i < payAccountBean.getData().getAliAccount().size(); i++) {
                                forList.add(payAccountBean);
                            }
                            setData(forList);
                            for (int i = 0; i < payAccountBean.getData().getBankAccount().size(); i++) {
                                forList1.add(payAccountBean);
                            }
                            setData1(forList1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_withdraw_account;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    /**
     * 添加账号
     */
    @OnClick({R.id.add_withdraw_ll_account, R.id.add_withdraw_ll_idcard})
    public void onClickAddLLAccount(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            /**
             * 添加支付宝账号
             */
            case R.id.add_withdraw_ll_account:
                intent.setClass(getBaseContext(), AddAlipayAccountActivity.class);
                startActivityForResult(intent, 118);
                break;
            /**
             * 添加银行卡账号
             */
            case R.id.add_withdraw_ll_idcard:
                intent.setClass(getBaseContext(), AddAlipayAccountIdCardActivity.class);
                startActivityForResult(intent, 119);
                break;
        }


    }

    private void setData(List<PayAccountBean> foreshowList) {
        for (PayAccountBean vo : foreshowList) {
            listItems.add(vo);
        }
        withdrawAccountAdapter.notifyDataSetChanged();

    }

    private void setData1(List<PayAccountBean> foreshowList) {
        for (PayAccountBean vo : foreshowList) {
            listItems1.add(vo);
        }
        withdrawAccountIdCardAdapter.notifyDataSetChanged();

    }

    /**
     * 删除支付宝账号
     *
     * @param id
     */
    private void initViewDeleteAliAccount(int id) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestDeleteAliAccount(
                UserUtils.getUserToken(), id, new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(WithdrawAccountActivity.this);
                    }

                    @Override
                    public void onCompleted() {
                        LoadingDialog.cancelDialogForLoading();

                    }

                    @Override
                    public void onError(Throwable e) {
                        LoadingDialog.cancelDialogForLoading();
                        ToastUitl.showShort(e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {
                                ToastUitl.showShort(baseBean.getError_message());
                                initPresenter();
                            } else if (baseBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    /**
     * 删除银行卡账号
     *
     * @param id
     */
    private void initViewDeleteBankAccount(int id) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestDeleteBankAccount(
                UserUtils.getUserToken(), id, new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(WithdrawAccountActivity.this);
                    }

                    @Override
                    public void onCompleted() {
                        LoadingDialog.cancelDialogForLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        LoadingDialog.cancelDialogForLoading();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            Gson gson = new Gson();
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {
                                ToastUitl.showShort(baseBean.getError_message());
                                initPresenter();
                            } else if (baseBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(baseBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPresenter();

    }
}
